package com.example.myapplication11_5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.TimePickerDialog;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取按钮和显示时间的TextView控件
        Button btnTimePicker = findViewById(R.id.btn_time_picker);
        TextView tvTimeResult = findViewById(R.id.tv_time_result);
        // 设置按钮点击事件，弹出时间选择对话框
        btnTimePicker.setOnClickListener(v -> {
            int hour = 12; // 默认小时
            int minute = 0; // 默认分钟
            // 创建时间选择对话框
            TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,3, (view, hourOfDay, minute1) -> {
                // 选择时间后，设置TextView显示所选时间
                String time = hourOfDay + "点" + minute1 + "分";
                tvTimeResult.setText("选择的时间：" + time);
            }, hour, minute, true);
            timePickerDialog.setTitle("设置时间"); // 设置对话框标题
            timePickerDialog.show(); // 显示对话框
        });
    }
}